package com.yaxin.heshanghui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.framework.Utils.PreferencesHelper;
import com.framework.Utils.ScreenUtils;
import com.framework.Utils.Utils;
import com.framework.Utils.Y;
import com.yaxin.heshanghui.R;
import com.yaxin.heshanghui.adapter.BiuooCommonAdapter;
import com.yaxin.heshanghui.adapter.BiuooListItem;
import com.yaxin.heshanghui.adapter.BiuooViewHolder;
import com.yaxin.heshanghui.base.BaseOnClickListener;
import com.yaxin.heshanghui.base.BiuooBaseActivity;
import com.yaxin.heshanghui.customview.CustomFlowLayout;
import com.yaxin.heshanghui.customview.CustomListView;
import com.yaxin.heshanghui.customview.TitleView;
import com.yaxin.heshanghui.utils.BiuooCollectionUtil;
import com.yaxin.heshanghui.utils.BiuooCustomProgressDialogUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class BiuooGuideActivity extends BiuooBaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback {

    private TitleView titleView;
    private CustomFlowLayout flowLayout;
    private CustomFlowLayout flowLayout2;
    private CustomListView customLv1;
    private CustomListView customLv2;
    private BiuooCommonAdapter<BiuooListItem> adapter1;
    private BiuooCommonAdapter<BiuooListItem> adapter2;
    private List<BiuooListItem> list1 = new ArrayList<>();
    private List<BiuooListItem> list2 = new ArrayList<>();
    private boolean _checked;
    String[] array;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.setTranslucentStatus(this, true);
        Utils.setStatusBarTintDrawable(this, R.mipmap.biuoo_bg_top);
        setContentView(R.layout.biuoo_activity_guide_buy);
        array = getResources().getStringArray(R.array.biuoo_phone);
        initView();
        BiuooCustomProgressDialogUtils.showProcessDialog(this, "正在请求");
        Utils.h.postDelayed(new Runnable() {
            @Override
            public void run() {
                BiuooCustomProgressDialogUtils.dismissProcessDialog();
                initFlowLayout(flowLayout, initData1());
                initFlowLayout(flowLayout2, initData2());
                initList1();
                initList2();
            }
        }, 1000);
    }


    private void initList1() {
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            BiuooListItem item = new BiuooListItem();
            item.setTitle("OPPO R9 柔光自拍 64G 白色");
            item.setImgRes(R.mipmap.biuoo_icon_phone_oppo_r9);
            item.setPrice("¥2799.00");
            item.set_price(2799.00);
            item.set_count(512);
            item.setCount("库存:" + 512);
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_m6s);
            item2.setTitle("Meitu/美图M6S自拍美颜手机");
            item2.setPrice("¥2899.00");
            item2.set_price(2899.00);
            item2.set_count(56);
            item2.setCount("库存:" + 56);
            list1.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            BiuooListItem item = new BiuooListItem();
            item.setTitle("金立M2017  128G");
            item.setImgRes(R.mipmap.biuoo_icon_phone_jinlim2017);
            item.setPrice("¥6999.00");
            item.set_price(6999.00);
            item.set_count(79);
            item.setCount("库存:" + 79);
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_p10);
            item2.setTitle("华为P10 钻雕金64G");
            item2.setPrice("¥3788.00");
            item2.set_price(2899.00);
            item2.set_count(201);
            item2.setCount("库存:" + 201);
            list1.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            BiuooListItem item = new BiuooListItem();
            item.setImgRes(R.mipmap.biuoo_icon_phone_rongyao8);
            item.setTitle("华为荣耀8青春版 32G");
            item.setPrice("¥1099.00");
            item.set_price(1099.00);
            item.setCount("库存:" + 99);
            item.set_count(99);
            list1.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_note4);
            item2.setTitle("红米note4 64G 银色");
            item2.setPrice("¥1099.00");
            item2.set_price(1099.00);
            item2.setCount("库存:" + 163);
            item2.set_count(163);
            list1.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            BiuooListItem item = new BiuooListItem();
            item.setImgRes(R.mipmap.biuoo_icon_phone_iphone7);
            item.setTitle("iPhone 7 玫瑰金 128G");
            item.setPrice("¥5699.00");
            item.set_price(5699.00);
            item.setCount("库存:" + 37);
            item.set_count(37);
            list1.add(item);
        }
        adapter1 = initAdapter(adapter1, true);
        customLv1.setAdapter(adapter1);
    }

    private void initList2() {
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            BiuooListItem item = new BiuooListItem();
            item.setImgRes(R.mipmap.biuoo_icon_phone_accessories_1_1);
            item.setTitle("外置柔光手机镜头");
            item.setPrice("¥129.00");
            item.set_price(129.00);
            item.setCount("库存:" + 366);
            item.set_count(366);
            list2.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_accessories_1_2);
            item2.setTitle("自拍杆");
            item2.setPrice("¥66.00");
            item2.set_price(166.00);
            item2.setCount("库存:" + 157);
            item2.set_count(157);
            list2.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            BiuooListItem item = new BiuooListItem();
            item.setImgRes(R.mipmap.biuoo_icon_phone_accessories_2_1);
            item.setTitle("华为P10原装手机壳");
            item.setPrice("¥129.00");
            item.set_price(129.00);
            item.setCount("库存:" + 65);
            item.set_count(65);
            list2.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_accessories_2_2);
            item2.setTitle("真皮商务手机壳");
            item2.setPrice("¥118.00");
            item2.set_price(118.00);
            item2.setCount("库存:" + 312);
            item.set_count(312);
            list2.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            BiuooListItem item = new BiuooListItem();
            item.setImgRes(R.mipmap.biuoo_icon_phone_accessories_3_1);
            item.setTitle("华为荣耀原装手机壳");
            item.setPrice("¥86.00");
            item.set_price(86.00);
            item.setCount("库存:" + 65);
            item.set_count(65);
            list2.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_accessories_3_2);
            item2.setTitle("飞利浦音乐耳机");
            item2.setPrice("¥79.00");
            item2.set_price(79.00);
            item2.setCount("库存:" + 313);
            item.set_count(313);
            list2.add(item2);
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            BiuooListItem item = new BiuooListItem();
            item.setImgRes(R.mipmap.biuoo_icon_phone_accessories_4_1);
            item.setTitle("iPhone7原装耳机");
            item.setPrice("¥139.00");
            item.set_price(139.00);
            item.setCount("库存:" + 65);
            item.set_count(65);
            list2.add(item);
            BiuooListItem item2 = new BiuooListItem();
            item2.setImgRes(R.mipmap.biuoo_icon_phone_accessories_4_2);
            item2.setTitle("iPhone7 小黄人手机壳");
            item2.setPrice("¥36.00");
            item2.set_price(36.00);
            item2.setCount("库存:" + 96);
            item.set_count(96);
            list2.add(item2);
        }

        adapter2 = initAdapter(adapter2, false);
        customLv2.setAdapter(adapter2);
    }

    private ArrayList<String> initData1() {
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            list.add("换机半年");
            list.add("爱好自拍");
            list.add("叠加包爱好者");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            list.add("换机一年");
            list.add("商务人士");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            list.add("换机半年");
            list.add("网聊达人");
            list.add("性价比控");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            list.add("换机一年");
            list.add("果粉");
            list.add("叠加包爱好者");
        }
        return list;
    }

    private ArrayList<String> initData2() {
        ArrayList<String> list = new ArrayList<>();
        if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[0])) {
            list.add("到店5次");
//             list.add("已收藏店铺");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[1])) {
            list.add("到店10次");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[2])) {
            list.add("到店7次");
        } else if (TextUtils.equals(PreferencesHelper.getInstance().getStringData("phone"), array[3])) {
            list.add("到店15次");
        }
        return list;
    }

    private BiuooCommonAdapter<BiuooListItem> initAdapter(BiuooCommonAdapter<BiuooListItem> adapter, boolean left) {
        if (adapter == null) {
            adapter = new BiuooCommonAdapter<BiuooListItem>(BiuooGuideActivity.this, left ? list1 : list2, R.layout.biuoo_listitem_guide) {
                @Override
                public View getListItemview(BiuooViewHolder viewHolder, View view, final BiuooListItem item, int position, ViewGroup parent) {
                    viewHolder.setText(R.id.tv_title, item.getTitle());
                    viewHolder.setText(R.id.tv_price, item.getPrice());
                    viewHolder.setText(R.id.tv_count, item.getCount());
                    viewHolder.setImageResource(R.id.iv_icon, item.getImgRes());
                    viewHolder.getView(R.id.biuoo_tv_check).setBackgroundResource(!item.isChecked() ? R.mipmap.biuoo_unchoose : R.mipmap.biuoo_choosed);
                    viewHolder.getConvertView().setOnClickListener(new BaseOnClickListener() {
                        @Override
                        protected void onBaseClick(View v) {
                            item.setChecked(!item.isChecked());
                            notifyDataSetChanged();
                            if (item.isChecked()) {
                                _checked = true;
                                titleView.setRightTxt("下一步");
                                return;
                            }
                            if (!BiuooCollectionUtil.getInstance().isEmpty(list1)) {
                                int len = list1.size();
                                for (int i = 0; i < len; i++) {
                                    if (list1.get(i).isChecked()) {
                                        _checked = true;
                                        titleView.setRightTxt("下一步");
                                        return;
                                    }
                                }
                            }
                            if (!BiuooCollectionUtil.getInstance().isEmpty(list2)) {
                                int len = list2.size();
                                for (int i = 0; i < len; i++) {
                                    if (list2.get(i).isChecked()) {
                                        _checked = true;
                                        titleView.setRightTxt("下一步");
                                        return;
                                    }
                                }
                            }
                            _checked = false;
                            titleView.setRightTxt("跳过");
                        }
                    });
                    return null;
                }
            };
        }
        return adapter;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_next:
                break;
            default:
                break;
        }
    }

    private void initView() {
        titleView = (TitleView) findViewById(R.id.titleView);
        titleView.setTitle("导购").setRightTxt("跳过").setRightClickListner(new BaseOnClickListener() {
            @Override
            protected void onBaseClick(View v) {
                Intent intent = new Intent(BiuooGuideActivity.this, BiuooGuideNextActivity.class);
                intent.putExtra("list1", (Serializable) list1);
                intent.putExtra("list2", (Serializable) list2);
                startActivity(intent);
                overridePendingTransition(R.anim.biuoo_slide_right_in,
                        R.anim.biuoo_slide_left_out);
//                startActivity(BiuooGuideNextActivity.class);
            }
        });
        flowLayout = (CustomFlowLayout) findViewById(R.id.flowLayout);
        flowLayout2 = (CustomFlowLayout) findViewById(R.id.flowLayout2);
        customLv1 = (CustomListView) findViewById(R.id.customLv1);
        customLv2 = (CustomListView) findViewById(R.id.customLv2);
    }

    private void initFlowLayout(CustomFlowLayout flowLayout, final List<String> list) {
        flowLayout.removeAllViews();
        int length = list.size();
        View[] views = new View[length];
        if (length > 0) {
            int _5dp = ScreenUtils.getInstance().dip2px(this, 5);
            for (int j = 0; j < length; j++) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT);
                final ViewGroup viewGroup = null;
                LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(BiuooGuideActivity.this).inflate(R.layout.biuoo_inflate_flowlayout,
                        viewGroup, false);
                TextView tv_title = (TextView) linearLayout.findViewById(R.id.tv_title);
                params.setMargins(_5dp, _5dp, _5dp, _5dp);
                tv_title.setLayoutParams(params);
                tv_title.setText(list.get(j));
                views[j] = linearLayout;
            }
            flowLayout.addData(views);
        }
    }
}
